package com.luych.oauth2Center.controller;

import com.luych.common.page.Page;
import com.luych.oauth2Center.entity.Oauth2Client;
import com.luych.oauth2Center.entity.Oauth2Scope;
import com.luych.oauth2Center.modal.Oauth2Code;
import com.luych.oauth2Center.modal.Oauth2Token;
import com.luych.oauth2Center.service.Oauth2Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class Oauth2Controller {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final Oauth2Service oauth2Service;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public Oauth2Controller(Oauth2Service oauth2Service, PasswordEncoder passwordEncoder) {
        this.oauth2Service = oauth2Service;
        this.passwordEncoder = passwordEncoder;
    }

    /**
     * Oauth2Client分页查询
     * @param page 分页参数
     * @param keywords 查询关键字
     * @return 分页结果
     */
    @GetMapping("oauth2/client")
    public Page oauth2ClientPage(Page page, String keywords) {
        return oauth2Service.pageClient(keywords, page);
    }

    /**
     * 新增Oauth2Client
     * @param oauth2Client Oauth2Client实体
     * @return 返回新增后反查的Oauth2Client
     */
    @PutMapping("oauth2/client")
    public Oauth2Client oauth2ClientAdd(@RequestBody Oauth2Client oauth2Client) {
        return oauth2Service.addClient(oauth2Client);
    }

    /**
     * 修改Oauth2Client
     * @param clientId Oauth2Client.clientId
     * @param oauth2Client Oauth2Client实体
     * @return 返回编辑后反查的Oauth2Client
     */
    @PostMapping( "oauth2/client/{clientId}")
    public Oauth2Client oauth2ClientModify(@PathVariable String clientId, @RequestBody Oauth2Client oauth2Client) {
        oauth2Client.setClientId(clientId);
        return oauth2Service.modifyClient(oauth2Client);
    }

    /**
     * 删除Oauth2Client
     * @param clientId Oauth2Client.clientId
     * @return Oauth2Client.clientId
     */
    @DeleteMapping("oauth2/client/{clientId}")
    public String oauth2ClientRemove(@PathVariable String clientId) {
        oauth2Service.removeClient(clientId);
        return clientId;
    }

    /**
     * 根据Oauth2Client.clientId查询Oauth2Client
     * @param clientId Oauth2Client.clientId
     * @return Oauth2Client
     */
    @GetMapping("oauth2/client/{clientId}")
    public Oauth2Client oauth2ClientByClientId(@PathVariable String clientId) {
        return oauth2Service.getByClientId(clientId);
    }

    /**
     * 根据Oauth2Client.clientId获取Token
     * @param clientId Oauth2Client.clientId
     * @return Oauth2Token
     */
    @GetMapping("oauth2/token/{clientId}")
    public Oauth2Token getToken(@PathVariable String clientId) {
        return oauth2Service.getToken(clientId);
    }


    /**
     * 保存accessToken
     * @param clientId 该token是谁申请的 Oauth2Client.clientId
     * @param accessToken accessToken内容
     * @param refreshToken refreshToken内容
     */
    @PutMapping("oauth2/token/{clientId}")
    public void saveToken(@PathVariable String clientId, @RequestParam String accessToken, String refreshToken, String scope) {
        oauth2Service.saveToken(clientId, accessToken, refreshToken, scope);
    }

    /**
     * 保存authenticationCode
     * @param clientId 该code是谁申请的 Oauth2Client.clientId
     * @param userId 已认证用户id
     * @param code authenticationCode
     * @param redirectUrl 回调地址
     * @param scope 授权范围
     */
    @PutMapping("oauth2/authentication/code/{clientId}/{code}")
    public void saveAuthenticationCode(@PathVariable String clientId, @PathVariable String code,
                                       @RequestParam String userId, @RequestParam String redirectUrl, String scope) {
        oauth2Service.saveAuthenticationCode(clientId, userId, code, redirectUrl, scope);
    }

    /**
     * 获取authenticationCode
     * @param clientId Oauth2Client.clientId
     * @param code 授权码
     * @return authenticationCode
     */
    @GetMapping("oauth2/authentication/code/{clientId}/{code}")
    public Oauth2Code getAuthenticationCode(@PathVariable String clientId, @PathVariable String code) {
        return oauth2Service.getAuthenticationCode(clientId, code);
    }

    /**
     * 删除authenticationCode
     * @param clientId Oauth2Client.clientId
     * @param code 授权码
     */
    @DeleteMapping("oauth2/authentication/code/{clientId}/{code}")
    public void deleteAuthenticationCode(@PathVariable String clientId, @PathVariable String code) {
        oauth2Service.deleteAuthenticationCode(clientId, code);
    }

    /**
     * 根据Oauth2Client.clientId获取允许的授权范围
     * @param clientId Oauth2Client.clientId
     * @return 允许的授权范围
     */
    @GetMapping("oauth2/scope/{clientId}")
    public List<Oauth2Scope> getScopeByClientId(@PathVariable String clientId) {
        return oauth2Service.getScopeByClientId(clientId);
    }
}
